package com.xinghai.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xinghai.common.utils.StringUtils;
import com.xinghai.system.domain.DingUser;
import com.xinghai.system.domain.XhVehicleInfoApplyRecord;
import com.xinghai.system.mapper.DingUserMapper;
import com.xinghai.system.mapper.XhVehicleInfoApplyRecordMapper;
import com.xinghai.system.service.IXhVehicleInfoApplyRecordService;
import org.apache.ibatis.session.ExecutorType;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Collections;
import java.util.List;
import java.util.Objects;

/**
 * 用车申请实例任务Service业务层处理
 * 
 * @author smy
 * @date 2024-07-25
 */
@Service
public class XhDriverInfoApplyRecordServiceImpl extends ServiceImpl<XhVehicleInfoApplyRecordMapper, XhVehicleInfoApplyRecord> implements IXhVehicleInfoApplyRecordService
{
    @Autowired
    private XhVehicleInfoApplyRecordMapper xhVehicleInfoApplyRecordMapper;

    @Autowired
    private SqlSessionFactory sqlSessionFactory;
    @Autowired
    private DingUserMapper dingUserMapper;

    /**
     * 查询用车申请实例任务
     * 
     * @param id 用车申请实例任务主键
     * @return 用车申请实例任务
     */
    @Override
    public XhVehicleInfoApplyRecord getInfo(Long id)
    {
        return xhVehicleInfoApplyRecordMapper.getInfo(id);
    }

    /**
     * 查询用车申请实例任务列表
     * 
     * @param xhDriverInfoApplyRecord 用车申请实例任务
     * @return 用车申请实例任务
     */
    @Override
    public List<XhVehicleInfoApplyRecord> list(XhVehicleInfoApplyRecord xhDriverInfoApplyRecord)
    {
        return xhVehicleInfoApplyRecordMapper.list(xhDriverInfoApplyRecord);
    }

    /**
     * 新增用车申请实例任务
     * 
     * @param xhDriverInfoApplyRecord 用车申请实例任务
     * @return 结果
     */
    @Override
    public int insert(XhVehicleInfoApplyRecord xhDriverInfoApplyRecord)
    {
        return xhVehicleInfoApplyRecordMapper.insert(xhDriverInfoApplyRecord);
    }
    /**
     * 批量新增用车申请实例任务
     *
     * @param xhDriverInfoApplyRecordList 用车申请实例任务
     * @return 结果
     */
    @Override
    public int insertBatch(List<XhVehicleInfoApplyRecord> xhDriverInfoApplyRecordList)
    {
        return xhVehicleInfoApplyRecordMapper.insertBatch(xhDriverInfoApplyRecordList);
    }
    /**
     * 修改用车申请实例任务
     * 
     * @param xhDriverInfoApplyRecord 用车申请实例任务
     * @return 结果
     */
    @Override
    public int update(XhVehicleInfoApplyRecord xhDriverInfoApplyRecord)
    {
        return xhVehicleInfoApplyRecordMapper.update(xhDriverInfoApplyRecord);
    }

    /**
     * 批量删除用车申请实例任务
     * 
     * @param ids 需要删除的用车申请实例任务主键
     * @return 结果
     */
    @Override
    public int deleteBatch(Long[] ids)
    {
        return xhVehicleInfoApplyRecordMapper.deleteBatch(ids);
    }

    /**
     * 删除用车申请实例任务信息
     * 
     * @param id 用车申请实例任务主键
     * @return 结果
     */
    @Override
    public int delete(Long id)
    {
        return xhVehicleInfoApplyRecordMapper.delete(id);
    }

    /**
     * 导入用车申请实例任务信息
     *
     * @param list 用车申请实例任务信息集合
     * @return 结果
     */
    @Override
    @Transactional
    public boolean importData(List<XhVehicleInfoApplyRecord> list){
        SqlSession sqlSession = sqlSessionFactory.openSession(ExecutorType.BATCH, false);
        try {
            XhVehicleInfoApplyRecordMapper xhDriverInfoApplyRecordMapper = sqlSession.getMapper(XhVehicleInfoApplyRecordMapper.class);
            list.stream().forEach(item -> xhDriverInfoApplyRecordMapper.insert(item));
            // 提交数据
            sqlSession.commit();
            sqlSession.rollback();
        } catch (Exception e) {
            sqlSession.rollback();
            return false;
        } finally {
            sqlSession.close();
        }
        return true;
    }

    @Override
    public List<XhVehicleInfoApplyRecord> getInfoByVehicleId(Long vehicleId) {
        LambdaQueryWrapper<XhVehicleInfoApplyRecord> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(XhVehicleInfoApplyRecord::getVehicleId, vehicleId);
        List<XhVehicleInfoApplyRecord> xhVehicleInfoApplyRecords = xhVehicleInfoApplyRecordMapper.selectList(queryWrapper);
        if (Objects.nonNull(xhVehicleInfoApplyRecords)) {
            xhVehicleInfoApplyRecords.forEach(item -> {
                if (StringUtils.isNotEmpty(item.getStaffId())) {
                    LambdaQueryWrapper<DingUser> dingUserWrapper =  new LambdaQueryWrapper<>();
                    dingUserWrapper.eq(DingUser::getUserid, item.getStaffId());
                    DingUser info = dingUserMapper.selectOne(dingUserWrapper);
                    if (Objects.nonNull(info)) {
                        item.setStaffId(info.getName());
                    }
                }
            });
        }
        return xhVehicleInfoApplyRecords;
    }
}
